﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home_Categories : System.Web.UI.Page
{
    public CategoryAdapter cateAdap;
    public PlatformAdapter platAdap;
    public List<PlatformEntity> platforms { set; get; }
    UserEntity currentUser;
    public LanguageManage Language;
    public string title = "Categories";
    public long categoryID;

    public CategoryEntity currentCategory = new CategoryEntity();
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check login
        currentUser = UserManager.Instance.Getuser();
        if (!currentUser.isLogin)
            Response.Redirect("~/Login.aspx");

        Language = new LanguageManage();
        btn_create.Text = Language["Addnewcategory"];        
        cateAdap = new CategoryAdapter();
        platAdap=new PlatformAdapter();
        ListCategory1.listCategory = cateAdap.getListCategories();       

        if (Request.QueryString["categoryid"] != null)
        {
            title ="Editcategory";
            btn_create.Text = Language["Update"];
            categoryID = Convert.ToInt32(Request.QueryString["categoryid"]);
            currentCategory = cateAdap.getCategory(categoryID)[0];
        }
       
        ListCategory1.isDisplayItem = false;

        platforms = platAdap.getListPlatform();

        Language = new LanguageManage();
        Session["platformid"] = null;
        if (Session["menu_id"] != null)
        {
            Session["menu_current"] = Session["menu_id"];
        }
        else
        {
            Session["menu_current"] = "catelories";
        }
        Session["menu_id"] = "catelories";



    }

    public string isCheckPlatform(long platformID)
    {
        try
        {
            long count = currentCategory.categoryDetails.Where(t => t.PlatformID == platformID).Count();
            if (count > 0)
                return "checked";
            else
            {
                return "";
            }
        }
        catch
        {
            return "";
        }
    }

    protected void btn_create_Click(object sender, EventArgs e)
    {
        CategoryAdapter cateAdap = new CategoryAdapter();
        
        try
        {
            currentCategory.Name = Request.Form["txtName"].ToString();
            currentCategory.Description = Request.Form["txtName"].ToString();
            List<CategoryDetailEntity> categoryDetails = new List<CategoryDetailEntity>();
            currentCategory.categoryDetails = categoryDetails;

            if (Request.QueryString["categoryid"] == null)
            {
                for (int i = 0; i < platforms.Count(); i++)
                {
                    if (Request.Form["platform_" + platforms[i].ID] != null)
                    {
                        CategoryDetailEntity detail = new CategoryDetailEntity();
                        detail.PlatformID = platforms[i].ID;
                        categoryDetails.Add(detail);

                    }
                }
                cateAdap.createCategory(currentCategory);
                Response.Redirect("~/Home/Categories.aspx");
            }
            else
            {
                for (int i = 0; i < platforms.Count(); i++)
                {
                    if (Request.Form["platform_" + platforms[i].ID] != null)
                    {
                        CategoryDetailEntity detail = new CategoryDetailEntity();
                        detail.PlatformID = platforms[i].ID;
                        detail.CategoryID = categoryID;
                        categoryDetails.Add(detail);

                    }
                }

                currentCategory.ID = categoryID;
                cateAdap.UpdateItem(currentCategory);
                Response.Redirect("~/Home/Categories.aspx");
            }
        }
        catch (Exception)
        {

            throw;
        }
 
        Response.Redirect("~/Home/Categories.aspx");
    }
}